
#include "xapp_config.h"

#include <string.h>
#include "os_timer.h"

#if USER_CHECK_ENABLED( TIMER )
#include "xapp_timer.h"
#endif 

#if USER_CHECK_ENABLED( STOPWATCH )
#include "xapp_stopwatch.h"
#endif

#if USER_CHECK_ENABLED( EMS )
#include "xapp_ems.h"
#endif 

#include "log.h"


#if USER_CHECK_ENABLED(  TIMER )
static void* xappTimer = NULL;
static void xapp_timer_timeout_handler(void* pxTimer)
{
    LOG_INFO1("[%s]: timer timeout \r\n", FORMAT_STRING( __FUNCTION__ ) );
    xapp_timer_task_update();
}

void xapp_timer_create(void)
{
    if( xappTimer == NULL )
    {
        os_timer_create( &xappTimer, "xapp_timer", 0, 100, false, xapp_timer_timeout_handler );
    }
}

void xapp_timer_start(uint32_t millseconds )
{
    if( xappTimer != NULL )
    {
        os_timer_restart( &xappTimer, millseconds );
    }
}
void xapp_timer_stop(void)
{
    if( xappTimer != NULL )
    {
        os_timer_stop( &xappTimer );
    }
}
#endif // !USER_CHECK_ENABLED( TIMER )

#if USER_CHECK_ENABLED( STOPWATCH )
static void* xappStopwatchTimer = NULL;
static void xapp_stopwatch_timer_timeout_handler(void* pxTimer)
{
    LOG_INFO1("[%s]: timer timeout \r\n", FORMAT_STRING( __FUNCTION__ ) );
    xapp_stopwatch_update();
}

void xapp_stopwatch_timer_create(void)
{
    if( xappStopwatchTimer == NULL )
    {
        os_timer_create( &xappStopwatchTimer, "stopwatch timer", 0, 100, false, xapp_stopwatch_timer_timeout_handler );
    }
}
void xapp_stopwatch_timer_start(uint32_t millseconds)
{
    if( xappStopwatchTimer != NULL )
    {
        os_timer_restart( &xappStopwatchTimer, millseconds );
    }
}

void xapp_stopwatch_timer_stop(void)
{
    if( xappStopwatchTimer != NULL )
    {
        os_timer_stop( &xappStopwatchTimer );
    }
}
#endif // !USER_CHECK_ENABLED( STOPWATCH )

#if USER_CHECK_ENABLED( EMS )
static void* xappEMSTimer = NULL;
static void xapp_EMS_timer_timeout_handler(void* pxTimer)
{
//    LOG_INFO1("[%s]: timer timeout \r\n", TRACE_STRING( __FUNCTION__ ) );
    xapp_ems_update();
}

void xapp_ems_timer_create(void)
{
    if( xappEMSTimer == NULL )
    {
        os_timer_create( &xappEMSTimer, "EMS timer", 0, 1000, true, xapp_EMS_timer_timeout_handler );
    }   
}
void xapp_ems_timer_start(void)
{
    if( xappEMSTimer != NULL )
    {
//        os_timer_restart( &xappEMSTimer, millseconds );
        os_timer_start( &xappEMSTimer );
    }
}

void xapp_ems_timer_stop(void)
{
    if( xappEMSTimer != NULL )
    {
        os_timer_stop( &xappEMSTimer );
    }
}
#endif // !USER_CHECK_ENABLED( STOPWATCH )
